﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using ComLib.ConfigService;
using ComLib.ComFun;

namespace NewCapturerDemo.Config
{
    [Serializable]
    [XmlRootAttribute("CameraOption", Namespace = "NewCapturerDemo.Config", IsNullable = false)]
    public class CameraOption : XmlSerializeBase
    {      
        public CameraOption() : base()
        {
        }

        /// <summary>
        /// 相机完全限定名称
        /// </summary>
        private string _monikerString = "";
        public string MonikerString
        {
            get { return _monikerString; }
            set { _monikerString = value; }
        }

        /// <summary>
        /// 相机索引
        /// </summary>
        private int _nIndex = 0;
        public int Index
        {
            get { return _nIndex; }
            set { _nIndex = value; }
        }

        /// <summary>
        /// 分辨率
        /// </summary>
        private Size _frameSize = new Size(2048, 1536);
        public Size FrameSize
        {
            get { return _frameSize; }
            set { _frameSize = value; }
        }

        /// <summary>
        /// 预览控件播放器分辨率
        /// </summary>
        private Size _videoPlayerSize = new Size(600, 720);
        public Size VideoPlayerSize
        {
            get { return _videoPlayerSize; }
            set { _videoPlayerSize = value; }
        }

        /// <summary>
        /// 相机名称
        /// </summary>
        private string _camName = "HD Camera";
        public string CamName
        {
            get { return _camName; }
            set { _camName = value; }
        }

        ///<summary>
        /// 旋转翻转值
        /// </summary>
        private RotateFlipType _rotateFlipValue = RotateFlipType.RotateNoneFlipX;
        public RotateFlipType RotateFlipValue
        {
            get { return _rotateFlipValue; }
            set { _rotateFlipValue = value; }
        }

        /// <summary>
        /// 原图上的有效区域
        /// </summary>
        private Rectangle _validRect = new Rectangle(350, 0, 1280, 1536);
        public Rectangle ValidRect
        {
            get { return _validRect; }
            set { _validRect = value; }
        }

        /// <summary>
        /// 是否使用单反相机模式
        /// </summary>
        private bool _isUseDslr = false;
        public bool IsUseDslr
        {
            get { return _isUseDslr; }
            set { _isUseDslr = value; }
        }

        /// <summary>
        /// 是否支持在预览时手动调节相机参数
        /// </summary>
        private bool _enableAdjustParams= false;
        public bool EnableAdjustParams
        {
            get { return _enableAdjustParams; }
            set { _enableAdjustParams = value; }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            CameraOption obj = (CameraOption)Serializer.LoadFromXml(ConfigFilePath, typeof(CameraOption));
            if (obj != null)
            {
                this._camName = obj._camName;
                this._monikerString = obj._monikerString;
                this._nIndex = obj._nIndex;
                this._nVer = obj._nVer;
                this._frameSize = obj._frameSize;
                this._rotateFlipValue = obj._rotateFlipValue;
                this._videoPlayerSize = obj._videoPlayerSize;
                this._validRect = obj._validRect;
                this._isUseDslr = obj._isUseDslr;
                this._enableAdjustParams = obj._enableAdjustParams;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public bool CheckValidRect(Size s)
        {
            if (_validRect.X + _validRect.Width > s.Width
                || _validRect.X + _validRect.Width <= 0)
            {
                ErrorMessage = string.Format("有效区域的X:{0}和宽度:{1}组合非法。因为相机分辨率为{2}*{3}.",
                    _validRect.X, _validRect.Width, s.Width, s.Height);
                return false;
            }

            if (_validRect.Y + _validRect.Height > s.Height
                || _validRect.Y + _validRect.Height <= 0)
            {
                ErrorMessage = string.Format("有效区域的Y:{0}和高度:{1}组合非法。因为相机分辨率为{2}*{3}.",
                   _validRect.Y, _validRect.Height, s.Width, s.Height);
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("拍照和预览选项: ");
            builder.Append("[相机索引]:" + Index.ToString());
            builder.Append("[相机完全限定名称]:" + _monikerString);
            builder.Append("[相机名称]:" + CamName);
            builder.Append("; [旋转翻转值]:" + _rotateFlipValue.ToString());           
            builder.Append("; [原图有效区域]:" + _validRect.ToString());
            builder.Append("; [镜头分辨率]:" + _frameSize.ToString());
            builder.Append("; [播放器分辨率]:" + _videoPlayerSize.ToString());
            builder.Append("; [是否使用单反]:" + _isUseDslr.ToString());
            builder.Append("; [是否支持手动调节相机参数]:" + _enableAdjustParams.ToString());
            return builder.ToString();
        }
    }
}
